package ar.com.gep.soccer.frontend.controllers;

import java.util.List;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import ar.com.cdt.framework.business.dto.BaseDTO;

public abstract class BaseManageBean {

	public static final String RESULT_TYPE_JSON = "json";
	public static final String RESULT_TYPE_STREAM = "stream";
	public static final String RESULT_TYPE_REDIRECT_ACTION = "redirectAction";
	public static final String LOGIN_ACTION = "LOGIN";
	public static final String DELETE_ACTION = "DELETE";
	public static final String DISABLE_ACTION = "DISABLE";
	public static final String ENABLE_ACTION = "ENABLE";
	public static final String SUCCESS_ACTION = "SUCCESS";
	public static final String VIEW_ACTION = "VIEW";
	public static final String UNSUCCESS_ACTION = null;
	public static final String ADD_ACTION = "ADD";
	public static final String EDIT_ACTION = "EDIT";
	public static final String DEACTIVATE_ACTION = "DEACTIVATE";
	public static final String SEARCH_ACTION = "SEARCH";
	public static final String SEARCH_PROJECT = "SEARCH_PROJECT";
	public static final String SEARCH_TASK = "SEARCH_TASK";
	public static final String SEARCH_CATEGORY = "SEARCH_CATEGORY";
	public static final String BACK = "BACK";
	public static final String CANCEL = "CANCEL";
	public static final String SEARCH_ROLE = "SEARCH_ROLE";
	public static final String SEARCH_USER = "SEARCH_USER";

	/* Key for flash messages */
	protected final String KEY_CUSTOM_FLASH = "custom_";
	protected final String KEY_TEAM = KEY_CUSTOM_FLASH.concat("team");

	private String staticPath;
	private String appVersion;
	private int maxPage;
	private int minPage;
	private int averagePage;
	private Long id;
	private String username;
	private Integer sprintExpiration;

	@PostConstruct
	public void initializeSession() {
		FacesContext context = FacesContext.getCurrentInstance();
		context.getExternalContext().getSession(true);
		this.staticPath = this.getPropertyValue("resources.path");
		this.appVersion = this.getPropertyValue("cdt.framework.application.version");
		this.maxPage = Integer.valueOf(this.getPropertyValue("primefaces.maxPage"));
		this.minPage = Integer.valueOf(this.getPropertyValue("primefaces.minPage"));
		this.averagePage = Integer.valueOf(this.getPropertyValue("primefaces.averagePage"));
		this.sprintExpiration = Integer.valueOf(this.getPropertyValue("primefaces.sprintExpiration"));

	}

	public String back() {
		return BACK;
	}

	public String getPropertyValue(final String key) {
		return ResourceBundle.getBundle("cdt-framework-frontend").getString(key);
	}

	public String getStaticPath() {
		return staticPath;
	}

	public void setStaticPath(String staticPath) {
		this.staticPath = staticPath;
	}

	public String getAppVersion() {
		return appVersion;
	}

	public void setAppVersion(String appVersion) {
		this.appVersion = appVersion;
	}

	public int getMaxPage() {
		return maxPage;
	}

	public void setMaxPage(int maxPage) {
		this.maxPage = maxPage;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getMinPage() {
		return minPage;
	}

	public void setMinPage(int minPage) {
		this.minPage = minPage;
	}

	public int getAveragePage() {
		return averagePage;
	}

	public void setAveragePage(int averagePage) {
		this.averagePage = averagePage;
	}

	public Integer getSprintExpiration() {
		return sprintExpiration;
	}

	public void setSprintExpiration(Integer sprintExpiration) {
		this.sprintExpiration = sprintExpiration;
	}

	/**
	 * Comprueba si un dto esta en una lista de dto
	 * 
	 * @param listDTOs {@link List}
	 * @param dto {@link BaseDTO}
	 * @return
	 */
	public boolean existsDTO(List<? extends BaseDTO> listDTOs, BaseDTO dto) {
		if (!listDTOs.isEmpty()) {
			if (dto.getId() != null) {
				// id != null entonces es una edicion
				if (!listDTOs.get(0).getId().equals(dto.getId())) {
					return true;
				}
			} else {
				return true;
			}
		}
		return false;
	}

	/**
	 * Verifica si el usuario conectado tiene el permiso de auditor
	 * 
	 * @return : {@link Boolean}
	 */
	protected Boolean userIsAuditor() {
		List<? extends GrantedAuthority> listPermissions = (List<? extends GrantedAuthority>) SecurityContextHolder.getContext().getAuthentication().getAuthorities();
		for (GrantedAuthority permission : listPermissions) {
			if (permission.toString().equals("AUDIT_AUDIT")) {
				return true;
			}
		}
		return false;
	}

}
